//定义js命名空间
Ext.namespace('FsCarInfo.FsCarbaseinfo');

//DateTime -- date , decimal -- float
//mapping : 'Department.Name'
//FsCarbaseinfo FieldArray
FsCarInfo.FsCarbaseinfo.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'ICCardUId', type: 'string' },
	{ name: 'KeyICCardUId', type: 'string' },
	{ name: 'DepartmentId', type: 'string' },
	{ name: 'Department.Name', mapping: 'Department.Name' },
	{ name: 'Brand', type: 'string' },
	{ name: 'Series', type: 'string' },
	{ name: 'Model', type: 'string' },
	{ name: 'Color', type: 'string' },
	{ name: 'EnginId', type: 'string' },
	{ name: 'FrameId', type: 'string' },
	{ name: 'ProdDate', type: 'date' },
	{ name: 'Mileage', type: 'int' },
	{ name: 'MarketPrice', type: 'float' },
	{ name: 'StoreDate', type: 'date' },
	{ name: 'AtmGjb', type: 'int' },
	{ name: 'AtmBt', type: 'int' },
	{ name: 'AtmSms', type: 'int' },
	{ name: 'AtmDyq', type: 'int' },
	{ name: 'AtmBxk', type: 'int' },
	{ name: 'AtmKey', type: 'int' },
	{ name: 'AtmMast', type: 'int' },
	{ name: 'AtmWiper', type: 'int' },
	{ name: 'AtmCertificate', type: 'int' },
	{ name: 'CustId', type: 'string' },
	{ name: 'State', type: 'int' },
	{ name: 'Description', type: 'string' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'CreateTime', type: 'date' }
);

//FsCarbaseinfo JsonReader
FsCarInfo.FsCarbaseinfo.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsCarbaseinfo.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FsCarInfo.FsCarbaseinfo.BaseFieldsArray
		}, cfg));
	}
});

//FsCarbaseinfo JsonStore
FsCarInfo.FsCarbaseinfo.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsCarbaseinfo.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/FsCarbaseinfo/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields: FsCarInfo.FsCarbaseinfo.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/FsCarbaseinfo/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//FsCarbaseinfo GridPanel
FsCarInfo.FsCarbaseinfo.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	//提交保存Url
	baseUrl: '/FsCarbaseinfo/',
	formSubmitUrl: '/FsCarbaseinfo/SaveOrUpdate',
	//提交保存提示信息
	formSubmitMsg: '确定要保存商品车信息吗?',
	//工具栏标签
	tbarMask: '0',
	//表格列默认显示
	columnMask: '012345600001230000090123000000',
	//状态
	carState: '',
	store: new FsCarInfo.FsCarbaseinfo.JsonStore(),

	//constructor
	initComponent: function () {
		// prepare
		var itemGrid = this;
		var cmdAddEntity = this.onAddEntity;
		var cmdPrintQrCode = this.onPrintQrCode;

		this.store.setBaseParam('state', this.carState);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [
				{ dataIndex: 'Department.Name', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.DeptStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'Brand', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.CarBrandStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'Series', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.CarSeriesStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'Model', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.CarModelStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'Color', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.CarColorStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'EnginId', type: 'string' },
				{ dataIndex: 'FrameId', type: 'string' },
				{ dataIndex: 'ProdDate', type: 'date' },
				{ dataIndex: 'Mileage', type: 'int' },
				{ dataIndex: 'MarketPrice', type: 'int' },
				{ dataIndex: 'StoreDate', type: 'date' },
				{ dataIndex: 'AtmGjb', type: 'string' },
				{ dataIndex: 'AtmBt', type: 'string' },
				{ dataIndex: 'AtmSms', type: 'string' },
				{ dataIndex: 'AtmDyq', type: 'string' },
				{ dataIndex: 'AtmBxk', type: 'string' },
				{ dataIndex: 'AtmKey', type: 'string' },
				{ dataIndex: 'AtmMast', type: 'string' },
				{ dataIndex: 'AtmWiper', type: 'string' },
				{ dataIndex: 'AtmCertificate', type: 'string' },
				{ dataIndex: 'State', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.CarStateStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'Createtime', type: 'date' }
			]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			//header: '',
			//singleSelect: true,
			menuDisabled: true,
			sortable: false,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					sm.grid.delBtn.setDisabled(sm.getCount() < 1);
					if (itemGrid.stoBtn != null) {
						itemGrid.stoBtn.setDisabled(sm.getCount() < 1);
						itemGrid.btnPrintQrCode.setDisabled(sm.getCount() < 1);
						itemGrid.editBtn.setDisabled(sm.getCount() != 1);
						var s = itemGrid.getSelectionModel().getSelections();
						for (var i = 0, r; r = s[i]; i++) {
							if (r.data.State != 0) {
								itemGrid.stoBtn.setDisabled(true);
								itemGrid.delBtn.setDisabled(true);
								break;
							}
						}
					}
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{ header: '展场', dataIndex: 'Department.Name' },
				{ header: '品牌', dataIndex: 'Brand' },
				{ header: '系列', dataIndex: 'Series' },
				{ header: '型号', dataIndex: 'Model' },
				{ header: '颜色', dataIndex: 'Color' },
				{ header: '发动机号', dataIndex: 'EnginId', hidden: true },
				{ header: '车架号', dataIndex: 'FrameId', hidden: true },
				{ header: '出厂日期', dataIndex: 'ProdDate', xtype: 'datecolumn', format: 'Y-m-d' },
				{ header: '里程', dataIndex: 'Mileage', xtype: 'numbercolumn', align: 'right' },
				{ header: '市场指导价', dataIndex: 'MarketPrice', xtype: 'numbercolumn', align: 'right' },
				{ header: '入库日期', dataIndex: 'StoreDate', xtype: 'datecolumn', format: 'Y-m-d' },
				{ header: '工具包', dataIndex: 'AtmGjb', hidden: true },
				{ header: '备胎', dataIndex: 'AtmBt', hidden: true },
				{ header: '说明书', dataIndex: 'AtmSms', hidden: true },
				{ header: '点烟器', dataIndex: 'AtmDyq', hidden: true },
				{ header: '保修卡', dataIndex: 'AtmBxk', hidden: true },
				{ header: '车钥匙', dataIndex: 'AtmKey', hidden: true },
				{ header: '天线', dataIndex: 'AtmMast', hidden: true },
				{ header: '雨刮', dataIndex: 'AtmWiper', hidden: true },
				{ header: '合格证', dataIndex: 'AtmCertificate', hidden: true },
				{ header: '状态', dataIndex: 'State',
					renderer: function (v) {
						return FsCarInfo.Params.CarStateStore.getById(v).get('Name');
					}
				},
				{ header: '备注', dataIndex: 'Description', hidden: true }
			],
			defaults: { sortable: true }
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		var toolBar = [{
			xtype: 'tbaraddbtn',
			handler: cmdAddEntity,
			scope: itemGrid
		}, {
			xtype: 'tbardelbtn',
			handler: function () {
				itemGrid.delEntity();
			}
		}, {
			xtype: 'tbareditbtn',
			handler: function () {
				itemGrid.onLoadEntity(itemGrid.getSelectionModel().getSelected(), true);
			}
		}, {
			hidden: this.tbarMask.charAt(1) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'tbbutton',
			hidden: this.tbarMask.charAt(2) != '1',
			minWidth: 80,
			ref: '../stoBtn',
			text: '入库',
			cls: 'x-btn-text-icon',
			disabled: true,
			icon: '/Content/icons/accept.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'CarStorageIn', '确定要将选定车辆入库吗?', '车辆信息入库成功。');
			}
		}, {
			hidden: this.tbarMask.charAt(2) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'tbbutton',
			hidden: this.tbarMask.charAt(3) != '1',
			minWidth: 80,
			ref: '../btnPrintQrCode',
			text: '打印条码',
			cls: 'x-btn-text-icon',
			disabled: true,
			icon: '/Content/icons/qr_code.png',
			handler: cmdPrintQrCode,
			scope: itemGrid
		}, {
			hidden: this.tbarMask.charAt(3) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'textfield',
			width: 150,
			id: 'q_query',
			ref: '../q_query',
			listeners: {
				specialkey: function (field, e) {
					if (e.getKey() == e.ENTER) {
						itemGrid.qryList(field.getValue());
					}
				}
			}
		}, {
			xtype: 'tbarqrybtn',
			handler: function (e) {
				itemGrid.qryList(itemGrid.q_query.getValue());
			},
			scope: itemGrid
		}, '-'];

		Ext.apply(this, {
			plugins: [gridFilter],
			sm: selModel,
			cm: gridColumn,
			tbar: toolBar,
			bbar: pagebBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onLoadEntity(grid.store.getAt(rowIndex), false);
				}
			}
		});

		FsCarInfo.FsCarbaseinfo.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onAddEntity: function () {
		var itemGrid = this;
		var itemEditForm = new FsCarInfo.FsCarbaseinfo.EditForm({
			isEdit: true,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			title: '商品车信息-录入',
			iconCls: 'car_add',
			width: 600,
			items: [itemEditForm],
			buttons: [new Ext.ux.SubmitBtn({
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				}
			}), new Ext.ux.CancelBtn({
				handler: function () {
					itemEditWin.destroy();
				}
			})]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function (selRecord, isEdit) {
		var itemGrid = this;
		var selId = selRecord.data.Id;
		var itemEditForm = new FsCarInfo.FsCarbaseinfo.EditForm({
			isEdit: isEdit,
			submitUrl: this.formSubmitUrl,
			itemId: selId,
			dataRecord: selRecord,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
						JsHelper.ShowInfo('商品车信息更新成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			title: '商品车信息',
			iconCls: 'car',
			width: 600,
			items: [itemEditForm],
			buttons: [new Ext.ux.SubmitBtn({
				hidden: !(isEdit && itemEditForm.submitUrl != ''),
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				scope: itemEditForm
			}), new Ext.ux.CancelBtn({
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			})]
		});
		itemEditWin.show();
		itemEditForm.form.loadRecord(selRecord);
	},

	//打印二维码
	onPrintQrCode: function () {
		var itemGrid = this;
		var carQrCodeView = new FsCarInfo.FsCarbaseinfo.DataView();

		var s = itemGrid.getSelectionModel().getSelections();
		carQrCodeView.store.removeAll();
		for (var i = 0, r; r = s[i]; i++) {
			carQrCodeView.store.add(r);
		}

		var panel = new Ext.Panel({
			id: 'images-view',
			frame: true,
			width: 650,
			//height : 600,
			autoHeight: true,
			layout: 'fit',
			header: false,
			items: [carQrCodeView]
		});

		var panel_outer = new Ext.Panel({
			width: 680,
			height: 500,
			autoScroll: true,
			tbar: [{
				xtype: 'tbarprintbtn',
				handler: function () {
					Ext.ux.Printer.print(panel_outer);
				}
			}, '-', {
				xtype: 'btncancle',
				handler: function () {
					itemEditWin.destroy();
				}
			}],
			items: [panel]
		});

		var itemEditWin = new Ext.Window({
			title: '商品车二维码打印',
			iconCls: 'car',
			width: 700,
			autoHeight: true,
			resizable: false,
			constrain: true,
			modal: true,
			closeAction: 'close',
			plain: true,
			buttonAlign: 'center',
			items: [panel_outer]
		});
		itemEditWin.show();
	}
});

// 二维码打印DataView
FsCarInfo.FsCarbaseinfo.DataView = Ext.extend(Ext.DataView, {
	autoHeight: true,
	multiSelect: true,
	overClass: 'x-view-over',
	itemSelector: 'div.thumb-wrap',
	emptyText: 'No images to display',
	store: new FsCarInfo.FsCarbaseinfo.JsonStore(),

	plugins: [
		new Ext.DataView.DragSelector(),
		new Ext.DataView.LabelEditor({ dataIndex: 'name' })
	],

	initComponent: function () {
		Ext.applyIf(this, {
			tpl: new Ext.XTemplate(
				'<tpl for="."><div class="thumb-wrap" id="{Id}">',
				'<div class="thumb"><img width="150" height="150" src="/FsCarbaseinfo/GetCarQrCodeById?size=150&Id={Id}"></div>',
				'<span> [品牌]:{Brand}</span></div>',
				'</tpl>',
				'<div class="x-clear"></div>'
			)
		});

		FsCarInfo.FsCarbaseinfo.DataView.superclass.initComponent.call(this);
	}
});

//FsCarbaseinfo EditForm
FsCarInfo.FsCarbaseinfo.EditForm = Ext.extend(Infrastructure.BaseForm, {
	isEdit: false, //是否允许编辑
	submitUrl: '/FsCarbaseinfo/SaveOrUpdate',

	initComponent: function () {
		var itemEditForm = this;
		if (this.dataRecord != null)
			this.dataId = this.dataRecord.data.Id;

		//Form第一列
		// 定义表单列
		var firstColumn = [{
			fieldLabel: '整车ID卡',
			maxLength: 200,
			xtype: 'textfield',
			name: 'ICCardUId'
		}, {
			fieldLabel: '车钥匙ID卡',
			maxLength: 200,
			xtype: 'textfield',
			name: 'KeyICCardUId'
		}, {
			fieldLabel: '品牌',
			maxLength: 20,
			xtype: 'diccombo',
			store: FsCarInfo.Params.CarBrandStore,
			name: 'Brand'
		}, {
			fieldLabel: '系列',
			maxLength: 20,
			xtype: 'diccombo',
			store: FsCarInfo.Params.CarSeriesStore,
			name: 'Series'
		}, {
			fieldLabel: '型号',
			maxLength: 20,
			xtype: 'diccombo',
			store: FsCarInfo.Params.CarModelStore,
			name: 'Model'
		}, {
			fieldLabel: '颜色',
			maxLength: 20,
			xtype: 'diccombo',
			store: FsCarInfo.Params.CarColorStore,
			name: 'Color'
		}, {
			fieldLabel: '出厂日期',
			xtype: 'datefield',
			format: 'Y-m-d',
			name: 'ProdDate'
		}, {
			fieldLabel: '发动机号',
			maxLength: 30,
			xtype: 'textfield',
			name: 'EnginId'
		}, {
			fieldLabel: '车架号',
			maxLength: 30,
			xtype: 'textfield',
			name: 'FrameId'
		}, {
			fieldLabel: '里程',
			xtype: 'numberfield',
			name: 'Mileage'
		}, {
			fieldLabel: '市场指导价',
			xtype: 'numberfield',
			name: 'MarketPrice'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: '入库日期',
			xtype: 'datefield',
			format: 'Y-m-d',
			name: 'StoreDate'
		}, {
			fieldLabel: '展场',
			maxLength: 36,
			xtype: 'textfield',
			name: 'Department.Name'
		}, {
			fieldLabel: '状态',
			allowBlank: true,
			xtype: 'infra_localcombo',
			store: FsCarInfo.Params.CarStateStore,
			name: 'State'
		}, {
			fieldLabel: '二维码',
			xtype: 'qrcodeImage',
			src: '/FsCarbaseinfo/GetCarQrCodeById?size=150&Id=' + this.dataId
		}];

		// 定义表单单独列的组件对象集合
		var atmCheckboxGroup = new Ext.form.CheckboxGroup({
			autoHeight: true,
			layout: 'column',
			columns: 3,
			xtype: 'checkboxgroup',
			id: 'atmGroup',
			anchor: '100%',
			fieldLabel: '附件',
			//itemCls: 'x-check-group-alt',
			items: [{
				boxLabel: '全选',
				name: 'Atm_All',
				inputValue: '1',
				listeners: {
					'check': function () {
						var group = Ext.getCmp('atmGroup');
						var length = group.items.getCount();
						var all;
						if (this.checked == true) {
							all = true;
						} else {
							all = false;
						}
						for (i = 0; i < length; i++) {
							var colItems = group.items.get(i);
							colItems.setValue(all);
						}
					}
				}
			}, {
				boxLabel: '工具包', name: 'AtmGjb', inputValue: '1'
			}, {
				boxLabel: '车钥匙', name: 'AtmKey', inputValue: '1'
			}, {
				boxLabel: '备胎', name: 'AtmBt', inputValue: '1'
			}, {
				boxLabel: '点烟器', name: 'AtmDyq', inputValue: '1'
			}, {
				boxLabel: '雨刮', name: 'AtmWiper', inputValue: '1'
			}, {
				boxLabel: '天线', name: 'AtmMast', inputValue: '1'
			}, {
				boxLabel: '合格证', name: 'AtmCertificate', inputValue: '1'
			}, {
				boxLabel: '说明书', name: 'AtmSms', inputValue: '1'
			}, {
				boxLabel: '保修卡', name: 'AtmBxk', inputValue: '1'
			}]
		});

		var singleColumn = [atmCheckboxGroup,
		{
			xtype: 'textarea',
			fieldLabel: '描述',
			name: 'Description',
			readOnly: !this.isEdit,
			height: 50,
			width: 430
		}];

		Ext.apply(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false,
						readOnly: !this.isEdit
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						readOnly: true
					},
					items: secondColumn
				}]
			}, singleColumn],
			//			buttons: [new Ext.ux.SubmitBtn({
			//				hidden: !(this.isEdit && itemEditForm.submitUrl != ''),
			//				handler: function () {
			//					itemEditForm.saveItem(itemEditForm.submitUrl)
			//				},
			//				scope: itemEditForm
			//			}), new Ext.ux.CancelBtn({
			//				handler: function () {
			//					itemEditForm.ownerCt.destroy();
			//				}
			//			})],
			reader: new FsCarInfo.FsCarbaseinfo.JsonReader()
		});

		FsCarInfo.FsCarbaseinfo.EditForm.superclass.initComponent.call(this);
		//定义保存后执行的事件
		this.addEvents('afterSave');

		// CheckboxGroup 只能在控件生成后加载数据
		//		if (this.dataRecord != null) {
		//			this.form.loadRecord(this.dataRecord);
		//		}
	}
});

//商品车信息Win
FsCarInfo.FsCarbaseinfo.InfoWin = Ext.extend(Infrastructure.BaseWindow, {
	//User Prop
	dataId: '',
	dataRecord: null,
	submitUrl: '/FsCarbaseinfo/SaveOrUpdate',
	editForm: new FsCarInfo.FsCarbaseinfo.EditForm({
		dataId: this.dataId,
		dataRecord: this.dataRecord,
		submitUrl: this.submitUrl
	}), //只读?
	//Pubic
	title: '商品车信息测试',
	iconCls: 'car',
	width: 600,
	//constructor
	initComponent: function () {
		//this.editForm.form.loadRecord(this.dataRecord);
		Ext.apply(this, {
			items: [this.editForm]
		});
		FsCarInfo.FsCarbaseinfo.InfoWin.superclass.initComponent.call(this);
	}
});